﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.codebump;

public partial class ReverseMortgage_ReverseMortgage : System.Web.UI.Page
{
    DataAccess App;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {

                if (Request.QueryString.HasKeys())
                {
                    if (Request.QueryString["listid"] != String.Empty)
                        Session["listid"] = Request.QueryString["listid"];
                    else
                        Session["listid"] = 1;


                    if (Request.QueryString["dnis"] != null)
                    {
                        Session["dnis"] = Request.QueryString["dnis"];
                    }
                    else
                    {
                        Session["dnis"] = "9999999999";
                    }

                    Session["servername"] = Request.QueryString["servername"];
                    Session["projectname"] = Request.QueryString["projectname"];
                    Session["campaignname"] = Request.QueryString["campaignname"];
                    Session["agentid"] = Request.QueryString["agentid"];
                    Session["agentfirst"] = Request.QueryString["agentfirst"];
                    Session["agentlast"] = Request.QueryString["agentlast"];
 

                    pnlContactData.Visible = true;
                    pnlCallData.Visible = false;
                    pnlAddressData.Visible = false;
                }

                App = new DataAccess();
                Session["App"] = App;
                WebWizard.ActiveStepIndex = 2;
                tbListid.Text = App.GetCallData("enterpriselistid");
                App.PutCallData("ScriptStart", System.DateTime.Now.ToString());
                App.PutCallData("AgentFirst", Session["agentfirst"].ToString());
                App.PutCallData("AgentLast", Session["agentlast"].ToString());


                App.CommitCallData();

            }
            else
            {
                App = (DataAccess)Session["App"];

            }
        }
        catch (Exception ex)
        {
            lblException.Visible = true;
            lblException.Text = "CONTACT IT/SUPERVISOR with the following information or screenshot:" + ex.ToString();
        }

    }

    
    //POST LOGIC - For Step or Start pages
    protected void WebWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {
        try
        {
            int page = WebWizard.ActiveStepIndex;


            if (page == WebWizard.WizardSteps.IndexOf(this.Greeting))
            {

                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.SelfOrOther);
                Name.Text = tbFirstName.Text;
                Name0.Text = tbFirstName.Text;
                Name1.Text = tbFirstName.Text;
                Name2.Text = tbFirstName.Text;
                Name3.Text = tbFirstName.Text;
                Name4.Text = tbFirstName.Text;
                Name5.Text = tbFirstName.Text;
                Name6.Text = tbFirstName.Text;
                return;
            }

            if (page == WebWizard.WizardSteps.IndexOf(this.SelfOrOther))
            {
                You.Text = RM_Self("you");
                You0.Text = RM_Self("you");
                You1.Text = RM_Self("you");
                You2.Text = RM_Self("you");
                You3.Text = RM_Self("you");
                You4.Text = RM_Self("you");
                You5.Text = RM_Self("you");
                Your.Text = RM_Self("your");
                Your0.Text = RM_Self("your");
                Your1.Text = RM_Self("your");
                Your2.Text = RM_Self("your");
                Your3.Text = RM_Self("your");
                Your4.Text = RM_Self("your");
                Them.Text = RM_Self("them");
                Them0.Text = RM_Self("them");
                 
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Questions);
                return;
            }

            if (page == WebWizard.WizardSteps.IndexOf(this.Questions))
            {
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.PhoneNumber);

                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.PhoneNumber))
            {

                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.JustSendTheInfo))
            {

                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.Rebuttals))
            {
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Rebuttals);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.FAQ))
            {
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.FAQ);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.Disposition))
            {

                return;
            }
        }
        catch (Exception ex)
        {
            lblException.Visible = true;
            lblException.Text = ex.ToString();
        }


    }

    //POST LOGIC for finish pages
    protected void WebWizard_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        try
        {
            int page = WebWizard.ActiveStepIndex;


            if (page == WebWizard.WizardSteps.IndexOf(this.Greeting))
            {

                return;
            }

            if (page == WebWizard.WizardSteps.IndexOf(this.SelfOrOther))
            {

                return;
            }

            if (page == WebWizard.WizardSteps.IndexOf(this.Questions))
            {
                Name.Text = tbFirstName.Text;
                Name0.Text = tbFirstName.Text;
                Name1.Text = tbFirstName.Text;
                Name2.Text = tbFirstName.Text;
                Name3.Text = tbFirstName.Text;
                Name4.Text = tbFirstName.Text;
                Name5.Text = tbFirstName.Text;
                Name6.Text = tbFirstName.Text;
                You.Text = RM_Self("you");
                You0.Text = RM_Self("you");
                You1.Text = RM_Self("you");
                You2.Text = RM_Self("you");
                You3.Text = RM_Self("you");
                You4.Text = RM_Self("you");
                You5.Text = RM_Self("you");
                Your.Text = RM_Self("your");
                Your0.Text = RM_Self("your");
                Your1.Text = RM_Self("your");
                Your2.Text = RM_Self("your");
                Your3.Text = RM_Self("your");
                Your4.Text = RM_Self("your");
                Them.Text = RM_Self("them");
                Them0.Text = RM_Self("them");
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.PhoneNumber))
            {

                App.PutCallData("Disposition", "SC");
                App.PutCallData("webusercode", "X2");
                validPrimaryPhone.Text = "Must Enter a Primary Phone Number";
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                
                
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.JustSendTheInfo))
            {

                App.PutCallData("Disposition", "SC");
                App.PutCallData("webusercode", "X1");

                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.Rebuttals))
            {

                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.FAQ))
            {

                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Disposition);
                return;
            }
            if (page == WebWizard.WizardSteps.IndexOf(this.Disposition))
            {

                return;
            }
        }
        catch (Exception ex)
        {
            lblException.Visible = true;
            lblException.Text = ex.ToString();
        }


    }

    //PRE-LOGIC
    protected void WebWizard_ActiveStepChanged(object sender, EventArgs e)
    {
        
        if (WebWizard.ActiveStep.ID.ToString() == "Greeting")
        {
            pnlContactData.Visible = true;
            pnlCallData.Visible = false;
            pnlAddressData.Visible = false;

        }
        if (WebWizard.ActiveStep.ID.ToString() == "SelfOrOther")
        {
            pnlContactData.Visible = true;
            pnlCallData.Visible = false;
            pnlAddressData.Visible = false;

        }
        if (WebWizard.ActiveStep.ID.ToString() == "Questions")
        {
            Name.Text = tbFirstName.Text;
            Name0.Text = tbFirstName.Text;
            Name1.Text = tbFirstName.Text;
            Name2.Text = tbFirstName.Text;
            Name3.Text = tbFirstName.Text;
            Name4.Text = tbFirstName.Text;
            Name5.Text = tbFirstName.Text;
            Name6.Text = tbFirstName.Text;
            You.Text = RM_Self("you");
            You0.Text = RM_Self("you");
            You1.Text = RM_Self("you");
            You2.Text = RM_Self("you");
            You3.Text = RM_Self("you");
            You4.Text = RM_Self("you");
            You5.Text = RM_Self("you");
            Your.Text = RM_Self("your");
            Your0.Text = RM_Self("your");
            Your1.Text = RM_Self("your");
            Your2.Text = RM_Self("your");
            Your3.Text = RM_Self("your");
            Your4.Text = RM_Self("your");
            Them.Text = RM_Self("them");
            Them0.Text = RM_Self("them");
            if (chkViewAddress.Checked == true)
            {
                pnlContactData.Visible = false;
                pnlCallData.Visible = false;
                pnlAddressData.Visible = true;
            }
            else
            {
                pnlContactData.Visible = false;
                pnlCallData.Visible = true;
                pnlAddressData.Visible = false;
                //chkViewAddress.Checked = false;
            }

        }
        if (WebWizard.ActiveStep.ID.ToString() == "PhoneNumber")
        {

            App.PutCallData("FirstName", tbFirstName.Text.Replace(",", ""));
            App.PutCallData("LastName", tbLastName.Text.Replace(",", ""));
            App.PutCallData("PrimaryPhone", tbPriPhone.Text.Replace(",", ""));
            App.PutCallData("AlternatePhone", tbSecPhone.Text.Replace(",", ""));
            App.PutCallData("TimeOfDay", tbTimeOfDay.Text);
            App.PutCallData("Email", tbEmail.Text.Replace(",", ""));
            App.PutCallData("SelfOrOther", tbSelfOrElse.SelectedValue.ToString().Replace(",", ""));
            App.PutCallData("TheirName", tbTheirName.Text);
            App.PutCallData("Relationship", tbRelationship.Text.Replace(",", ""));
            App.PutCallData("HomeStreet", tbHStreet.Text.Replace(",", ""));
            if (ddlCity.Enabled == true)
                App.PutCallData("HomeCity", ddlCity.SelectedItem.ToString().Replace(",", ""));
            App.PutCallData("HomeState", ddlState.SelectedValue.ToString().Replace(",", ""));
            App.PutCallData("HomeZip", tbZip.Text);
            App.PutCallData("MailingStreet", tbMStreet.Text.Replace(",", ""));
            App.PutCallData("MailingCity", tbMCity.Text.Replace(",", ""));
            App.PutCallData("MailingState", tbMState.Text.Replace(",", ""));
            App.PutCallData("MailingZip", tbMZip.Text.Replace(",", ""));
            App.PutCallData("Age", tbAge.Text.Replace(",", ""));
            App.PutCallData("AnyoneElse", tbAnyoneElse.SelectedValue.ToString());
            App.PutCallData("TheirAge", tbTheirAge.Text.Replace(",", ""));
            App.PutCallData("OwnHome", tbOwnHome.Text);
            App.PutCallData("ResType", tbResType.SelectedValue.ToString());
            App.PutCallData("IfOther", tbIfOther.Text.Replace(",", ""));
            App.PutCallData("PrimaryRes", tbPrimaryResidence.SelectedValue.ToString());
            App.PutCallData("ApxValue", tbAproxValue.Text.Replace(",", ""));
            App.PutCallData("Mortgage", tbMortgage.SelectedValue.ToString());
            App.PutCallData("MortgageAmount", tbMortgageBal.Text.Replace(",", ""));
            App.PutCallData("EquityLOC", tbEquityLOC.SelectedValue.ToString());
            App.PutCallData("EquityLOCOwed", tbEquityLOCOwed.Text.Replace(",", ""));
            App.CommitCallData();
            DispoConfirm1.Visible = true;
            pnlContactData.Visible = true;
            pnlCallData.Visible = false;
            pnlAddressData.Visible = false;
        }
        if (WebWizard.ActiveStep.ID.ToString() == "JustSendTheInfo")
        {
            App.PutCallData("FirstName", tbFirstName.Text.Replace(",", ""));
            App.PutCallData("LastName", tbLastName.Text.Replace(",", ""));
            App.PutCallData("PrimaryPhone", tbPriPhone.Text.Replace(",", ""));
            App.PutCallData("AlternatePhone", tbSecPhone.Text.Replace(",", ""));
            App.PutCallData("TimeOfDay", tbTimeOfDay.Text);
            App.PutCallData("Email", tbEmail.Text.Replace(",", ""));
            App.PutCallData("SelfOrOther", tbSelfOrElse.SelectedValue.ToString().Replace(",", ""));
            App.PutCallData("TheirName", tbTheirName.Text);
            App.PutCallData("Relationship", tbRelationship.Text.Replace(",", ""));
            App.PutCallData("HomeStreet", tbHStreet.Text.Replace(",", ""));
            if (ddlCity.Enabled == true)
                App.PutCallData("HomeCity", ddlCity.SelectedItem.ToString().Replace(",", ""));
            App.PutCallData("HomeState", ddlState.SelectedValue.ToString().Replace(",", ""));
            App.PutCallData("HomeZip", tbZip.Text);
            App.PutCallData("MailingStreet", tbMStreet.Text.Replace(",", ""));
            App.PutCallData("MailingCity", tbMCity.Text.Replace(",", ""));
            App.PutCallData("MailingState", tbMState.Text.Replace(",", ""));
            App.PutCallData("MailingZip", tbMZip.Text.Replace(",", ""));
            App.PutCallData("Age", tbAge.Text.Replace(",", ""));
            App.PutCallData("AnyoneElse", tbAnyoneElse.SelectedValue.ToString());
            App.PutCallData("TheirAge", tbTheirAge.Text.Replace(",", ""));
            App.PutCallData("OwnHome", tbOwnHome.Text);
            App.PutCallData("ResType", tbResType.SelectedValue.ToString());
            App.PutCallData("IfOther", tbIfOther.Text.Replace(",", ""));
            App.PutCallData("PrimaryRes", tbPrimaryResidence.SelectedValue.ToString());
            App.PutCallData("ApxValue", tbAproxValue.Text.Replace(",", ""));
            App.PutCallData("Mortgage", tbMortgage.SelectedValue.ToString());
            App.PutCallData("MortgageAmount", tbMortgageBal.Text.Replace(",", ""));
            App.PutCallData("EquityLOC", tbEquityLOC.SelectedValue.ToString());
            App.PutCallData("EquityLOCOwed", tbEquityLOCOwed.Text.Replace(",", ""));
            App.CommitCallData();
            DispoConfirm1.Visible = true;
            pnlContactData.Visible = true;
            pnlCallData.Visible = false;
            pnlAddressData.Visible = true;
        }
        if (WebWizard.ActiveStep.ID.ToString() == "Rebuttals")
        {
            pnlContactData.Visible = true;
            pnlCallData.Visible = false;
            pnlAddressData.Visible = false;
        }
        if (WebWizard.ActiveStep.ID.ToString() == "FAQ")
        {
            pnlContactData.Visible = true;
            pnlCallData.Visible = false;
            pnlAddressData.Visible = false;
        }
        if (WebWizard.ActiveStep.ID.ToString() == "Disposition")
        {
            validPrimaryPhone.Visible = false;
            try
            {
            lblNonOrderDispo.Visible = false;
            //lblNonOrderDispo2.Visible = false;
            DispoConfirm1.Visible = false;
            if (tbPriPhone.Text.Length < 10 && App.GetCallData("webusercode") == "X2")
            {
                validPrimaryPhone.Visible = true;
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.PhoneNumber);
            }
            else
            {
                //Put Text Box information into the in-memory table

                App.PutCallData("FirstName", tbFirstName.Text.Replace(",", ""));
                App.PutCallData("LastName", tbLastName.Text.Replace(",", ""));
                App.PutCallData("PrimaryPhone", tbPriPhone.Text.Replace(",", ""));
                App.PutCallData("AlternatePhone", tbSecPhone.Text.Replace(",", ""));
                App.PutCallData("TimeOfDay", tbTimeOfDay.Text);
                App.PutCallData("Email", tbEmail.Text.Replace(",", ""));
                App.PutCallData("SelfOrOther", tbSelfOrElse.SelectedValue.ToString().Replace(",", ""));
                App.PutCallData("TheirName", tbTheirName.Text);
                App.PutCallData("Relationship", tbRelationship.Text.Replace(",", ""));
                App.PutCallData("HomeStreet", tbHStreet.Text.Replace(",", ""));
                if (ddlCity.Enabled == true)
                    App.PutCallData("HomeCity", ddlCity.SelectedItem.ToString().Replace(",", ""));
                App.PutCallData("HomeState", ddlState.SelectedValue.ToString().Replace(",", ""));
                App.PutCallData("HomeZip", tbZip.Text);
                App.PutCallData("MailingStreet", tbMStreet.Text.Replace(",", ""));
                App.PutCallData("MailingCity", tbMCity.Text.Replace(",", ""));
                App.PutCallData("MailingState", tbMState.Text.Replace(",", ""));
                App.PutCallData("MailingZip", tbMZip.Text.Replace(",", ""));
                App.PutCallData("Age", tbAge.Text.Replace(",", ""));
                App.PutCallData("AnyoneElse", tbAnyoneElse.SelectedValue.ToString());
                App.PutCallData("TheirAge", tbTheirAge.Text.Replace(",", ""));
                App.PutCallData("OwnHome", tbOwnHome.Text);
                App.PutCallData("ResType", tbResType.SelectedValue.ToString());
                App.PutCallData("IfOther", tbIfOther.Text.Replace(",", ""));
                App.PutCallData("PrimaryRes", tbPrimaryResidence.SelectedValue.ToString());
                App.PutCallData("ApxValue", tbAproxValue.Text.Replace(",", ""));
                App.PutCallData("Mortgage", tbMortgage.SelectedValue.ToString());
                App.PutCallData("MortgageAmount", tbMortgageBal.Text.Replace(",", ""));
                App.PutCallData("EquityLOC", tbEquityLOC.SelectedValue.ToString());
                App.PutCallData("EquityLOCOwed", tbEquityLOCOwed.Text.Replace(",", ""));


                App.CommitCallData();
                Response.Redirect("../DispositionComplete.aspx");

            }
            }
            catch (Exception ex)
            {
                lblException.Visible = true;
                lblException.Text = ex.ToString();
            }
         }

    }
    protected void chkViewAddress_CheckedChanged(object sender, EventArgs e)
    {
        if (chkViewAddress.Checked == true)
        {
            pnlContactData.Visible = false;
            pnlCallData.Visible = false;
            pnlAddressData.Visible = true;
        }
        else
        {
            pnlContactData.Visible = false;
            pnlCallData.Visible = true;
            pnlAddressData.Visible = false;
        }


    }
    protected void chkSameAs_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            if (chkSameAs.Checked == true)
            {
                tbMStreet.Text = tbHStreet.Text;
                tbMStreet.Enabled = false;
                tbMCity.Text = ddlCity.SelectedItem.ToString();
                tbMCity.Enabled = false;
                tbMState.Text = ddlState.SelectedItem.ToString();
                tbMState.Enabled = false;
                tbMZip.Text = tbZip.Text;
                tbMZip.Enabled = false;
            }
            else
            {
                tbMStreet.Enabled = true;
                tbMCity.Enabled = true;
                tbMState.Enabled = true;
                tbMZip.Enabled = true;
            }
        }
        catch
        {
            //Do Nothing
        }
        

    }
    public string RM_Self(string pronoun)
    {
        string _pronoun = "you";
    if (tbSelfOrElse.Text == "Self")
    {
        switch (pronoun)
        {
            case "you":
                _pronoun = "you";
                return _pronoun;
            case "your":
                _pronoun = "your";
                return _pronoun;
            case "them":
                _pronoun = "you";
                return _pronoun;
        }
    }
    else
    {
        switch (pronoun)
        {
            case "you":
                _pronoun = "they";
                return _pronoun;
            case "your":
                _pronoun = "their";
                return _pronoun;
            case "them":
                _pronoun = "them";
                return _pronoun;
            default:
                _pronoun = "they";
                return _pronoun;
        }
    }
    return _pronoun;

    }
    protected void btnSearchZip_Click(object sender, EventArgs e)
    {

        ddlCity.Items.Clear();
        ddlCity.Enabled = true;

        if (System.Text.RegularExpressions.Regex.IsMatch(tbZip.Text.ToUpper(), @"(^\d{5}$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)"))
        {
            try
            {

                GeoPlaces g = new GeoPlaces();

                AuthenticationHeader authHeader = new AuthenticationHeader();
                authHeader.SessionID = "6JV2ZA4lgG1YRR+HG+JU27QWk9zI3rpRWY2iG2mzNPcdieEQXfQv0OUY0EZ+pOTxhwI1NCuF7b2oY+U/bozy0mhjFHmpaXvb";
                g.AuthenticationHeaderValue = authHeader;

                //GeoPlaceDistance[] places = g.GetPlacesWithin("Los Angeles", "CA", 10, GeoPlaceType.ZipCode);
                GeoPlaceDistance[] places = g.GetPlacesInside(tbZip.Text, "");

                for (int i = 0; i < places.Length; i++)
                {
                    GeoPlaceDistance place = places[i];
                    ListItem li = new ListItem(place.ToPlace, place.ToState + "," + place.ToPlace);
                    ddlCity.Items.Add(li);
                }
                

                ddlState.Items.Clear();
                ddlState.Items.Add(ddlCity.SelectedItem.Value.Split(",".ToCharArray())[0]);

                //ddlCity.SelectedIndex = 0;

                if (System.Text.RegularExpressions.Regex.IsMatch(tbZip.Text.ToUpper(), @"(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)"))
                {
                    ddlCountry.SelectedValue = "CANADA";
                }
                else
                {
                    ddlCountry.SelectedValue = "USA";
                }

                if (ddlCity.Items.Count == 1)
                {
                    ddlCity.Enabled = false;

                    //tbState.Text += ddlCity.SelectedItem.Value.ToString();
                }
                else if (ddlCity.Items.Count <= 0)
                {
                    ddlCity.Items.Add("No Cities Found");
                    ddlCity.Enabled = false;
                }
                else
                {
                }
            }
            catch
            {
                ddlCity.Items.Add("No Cities Found");
                ddlCity.Enabled = false;
            }
        }
        else
        {
            ddlCity.Items.Add("Invalid Zip");
            ddlCity.Enabled = false;
        }
        if (ddlCity.Enabled == true)
        {
            ddlCity.Items.Insert(0, "Select A City");
            ddlCity.SelectedIndex = 0;
        }
    }
    protected void WebWizard_PreviousButtonClick(object sender, WizardNavigationEventArgs e)
    {

        validPrimaryPhone.Visible = false;
    }
}